package net.wanho.shop.service.impl;

import lombok.RequiredArgsConstructor;
import net.wanho.shop.mapper.PermissionMapper;
import net.wanho.shop.po.Permission;
import net.wanho.shop.service.PermissionService;
import net.wanho.shop.vo.PermVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;
import java.util.stream.Collectors;


/**
 * <p>
 * 权限表 服务实现类
 * </p>
 *
 * @author 猛虎王
 * @since 2024-12-24
 */
@Service
@RequiredArgsConstructor
@Transactional(rollbackFor = Exception.class)
public class PermissionServiceImpl implements PermissionService {

    private final PermissionMapper permissionMapper;

    /**
     * 权限表分页查询
     *
     * @param pageNum
     * @param pageSize
     * @param permission
     * @return
     */
//    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
//    @Override
//    public PageInfo findPage(int pageNum, int pageSize, Permission permission) {
//        if (ObjectUtil.isNull(permission)) {
//            permission = new Permission();
//        }
//
//        // 分页
//        PageHelper.startPage(pageNum, pageSize);
//        List<Permission> permissionList = permissionMapper.select(permission);
//        return new PageInfo(permissionList);
//    }

    /**
     * 根据主键查询权限表
     *
     * @param psId
     * @return
     */
    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    @Override
    public Permission findById(Serializable psId) {
        return permissionMapper.selectById(psId);
    }

    /**
     * 权限表新增
     *
     * @param permission
     */
    @Override
    public void add(Permission permission) {
        permissionMapper.insert(permission);
    }

    /**
     * 修改权限表
     *
     * @param permission
     */
    @Override
    public void modify(Permission permission) {
        permissionMapper.update(permission);
    }

    /**
     * 根据主键删除权限表
     *
     * @param psId
     */
    @Override
    public void removeById(Serializable psId) {
        if (((String) psId).contains(",")) {
            permissionMapper.deleteByIds(((String) psId).split(","));
        } else {
            permissionMapper.deleteById(psId);
        }
    }

    @Override
    public List<PermVo> findType(String type) {
        List<PermVo> perms = permissionMapper.select(new Permission());
        if (type.equals("list")) {
            return perms;
        }

        if (type.equals("tree")) {
            List<PermVo> list = perms.stream()
                    .filter(item -> item.getPid() == 0)
                    .map(item -> {
                        // 递归查询子节点
                        findChildren(item, perms);
                        return item;
                    }).collect(Collectors.toList());

            return list;
        }
        return null;
    }

    public void findChildren(PermVo item, List<PermVo> perms) {
        List<PermVo> children = perms.stream()
                .filter(subItem -> subItem.getPid().equals(item.getId()))
                .map(subItem -> {
                    // 递归查询子节点
                    findChildren(subItem, perms);
                    return subItem;
                }).collect(Collectors.toList());
        item.setChildren(children);
    }
}
